from datetime import datetime, timedelta
import redis
from master import config, settings
import json


def create_uid_commands_list(uid_dict_list):
    commands_list = []
    url_header = 'https://twitter.com/'
    for uid_dict in uid_dict_list:
        command = {}
        command['type'] = 'TIMELINE'
        command['db'] = settings.target_db
        command['user_info'] = config.userinfo_collection
        command['collection'] = 'user_tweets'
        command['url'] = str.format(settings.userinfo_url, uid_dict['uid'])
        command['referer'] = url_header + uid_dict['uid']
        command['start_date'] = '1000-01-01'
        command['timeline_url'] = settings.timeline_url
        command['start_date'] = str(uid_dict['start_date'])
        command['start_time'] = str(uid_dict['start_time'])
        commands_list.append(command)
    return commands_list


# 执行两种分发任务：一是按照日期&关键词分发（关键词查找），二是按照uid及截止日期分发（个人时间线获取及更新）
if __name__ == '__main__':
    # 接受日期参数并分解任务，放入redis任务队列（queue：task）
    if len(settings.target_db) <= 0:
        print("未指定数据库")
        exit(0)

    commands_list = create_uid_commands_list(settings.user_list)

    redis_client = redis.Redis(host=config.redis_host, port=config.redis_port)
    if settings.clear_exist_cmd:
        redis_client.delete(config.task_queue_name)
    for command in commands_list:
        t = json.dumps(command)
        redis_client.lpush(config.task_queue_name, t)
        print(command)
    print(redis_client.llen(config.task_queue_name))
    print("任务已发送到redis队列！")
